package com.magist.manage.system.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.DateUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.magist.manage.system.entity.AdContent;
import com.magist.manage.system.dto.AdContentDto;
import com.magist.manage.system.service.AdContentService;

@Api(value = "RestAdContentController", description = "RestAdContentController接口")
@RestController
@RequestMapping("/rest/adContent")
public class RestAdContentController extends BaseController {

	@Autowired
	private AdContentService adContentService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("adContentDto") AdContentDto adContentDto) {
		AdContent adContent = new AdContent();
		BeanUtils.copyProperties(adContentDto,adContent);
		adContent.setId(adContentService.getNewId());
		adContentService.insert(adContent);
		return ResultMessage.success();
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("adContentDto") AdContentDto adContentDto) {
		AdContent oldAdContent = adContentService.getById(adContentDto.getId());
		if(oldAdContent==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		AdContent adContent = new AdContent();
		BeanUtils.copyProperties(adContentDto,adContent);
		adContent.setModifyTime(oldAdContent.getModifyTime());
		if (adContentService.update(adContent)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("adContentDto") AdContentDto adContentDto) {
		ResultMessage result = ResultMessage.success();
		AdContent adContent = new AdContent();
		BeanUtils.copyProperties(adContentDto,adContent);
		String adName = adContent.getAdName();
		String adCode = adContent.getAdCode();
		if(!StringUtils.isEmpty(adName)) {
			adContent.setAdName("%"+adName+"%");
		}
		if(!StringUtils.isEmpty(adCode)) {
			adContent.setAdCode("%"+adCode+"%");
		}
		PageInfo<AdContent> pageInfo = adContentService.page(adContent,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		AdContent adContent = adContentService.getById(id);
		if(adContent==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(adContentService.delete(adContent)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		AdContent adContent = adContentService.getById(id);
		if(adContent==null)
			throw new BusinessException("无效的数据!");
		AdContentDto adContentDto = new AdContentDto();
		BeanUtils.copyProperties(adContent,adContentDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(adContentDto);
		return result;
	}
}
